//
//  LWHomeViewController.m
//  LWShopItem
//
//  Created by mac on 17/3/14.
//  Copyright © 2017年 LWShop.com. All rights reserved.
//

#import "LWHomeViewController.h"
#import "SDCycleScrollView.h"
#import "UIColor+Addition.h"
#import "LWHomeModel.h"
#import "LWTopCell.h"
#import "LWHomeModel.h"
#import "LWGroupList.h"
#import "LWMidCell.h"
#import "LWNewestCell.h"
#import "LWPromotionCell.h"
#import "LWDetailsViewController.h"
#import "LWDetailModel.h"
#import "LWhigtGoodsCell.h"
#import <UIImageView+WebCache.h>
#import <CommonCrypto/CommonCrypto.h>
#import <AFNetworking.h>
#import "LWShopCarViewController.h"
#import "LWRichScanVc.h"

static NSString *topcell = @"topcell";
static NSString *hotcell = @"hotcell";
static NSString *newCell = @"newCell";
static NSString *promotionCell = @"promotionCell";
static NSString *highCell = @"highCell";

@interface LWHomeViewController ()<SDCycleScrollViewDelegate,LWMidCellDelegate,LWNewestCellDelegate,LWPromotionCellDelegate,LWhigtGoodsCellDelegate,CAAnimationDelegate>

@property(nonatomic, strong) SDCycleScrollView *sdcycleScrollerView;

@end

@implementation LWHomeViewController
{
    UIButton *chooseCityBtn;
    
    //热卖商品集合
    NSArray<LWGroupList *> *_hotGoodsList;
    
    //最新货物集合
    NSArray<LWGroupList *> *_newGoodsList;
    
    //促销货物集合
    NSArray<LWGroupList *> *_promotionList;
    
    //优质集合
    NSArray<LWGroupList *> *_highGoodsList;
    
    
    
}
//LWMidCellDelegate,LWNewestCellDelegate,LWPromotionCellDelegate
- (void)deleteFromShopCarWithModel:(LWGroupList *)model {
    if (model.orderCount > 0) {
        [_seleM enumerateObjectsUsingBlock:^(LWDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.goods_id isEqualToString:model.goods_id]) {
                obj.orderCount --;
            }
        }];
    } else {
        [_seleM enumerateObjectsUsingBlock:^(LWDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.goods_id isEqualToString:model.goods_id]) {
                [_seleM removeObject:obj];
            }
        }];
    }
}
- (void)addInToShopCarWithModel:(LWGroupList *)model {
    if (model.orderCount == 1) {
        NSString *secrt = @"shop.sdlinwang.com";
        NSDate *date = [NSDate dateWithTimeIntervalSinceNow:0];
        NSTimeInterval interval = [date timeIntervalSince1970];
        NSString *dateString = [NSString stringWithFormat:@"%0.f",interval];
        
        NSString *sing = [NSString stringWithFormat:@"%@%@%@",model.goods_id,dateString,secrt];
        
        
        AFHTTPSessionManager  *manager = [AFHTTPSessionManager manager];
        manager.responseSerializer = [AFHTTPResponseSerializer serializer];
        
        NSDictionary *dicArr = @{@"id":model.goods_id,@"time":dateString,@"sign":[self hybnetworking_md5:sing]};
        
        [manager POST:@"http://shop.sdlinwang.com/index.php?m=Api&c=Goods&a=goodsInfo" parameters:dicArr progress:nil success:^(NSURLSessionDataTask * _Nullable task, id _Nullable responseObject){
            
            //            NSLog(@"请求成功:%@", responseObject);
            
            NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            
            //            NSLog(@"请求成功JSON:%@", JSON);
            
            NSDictionary *tempDict = JSON[@"result"][@"goods"];
            
            //字典转模型
            LWDetailModel *detailModel = [LWDetailModel detilWtihDict:tempDict];
            detailModel.orderCount = 1;
            
            [_seleM addObject:detailModel];
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error){
            NSLog(@"%@",error);
        }];
        
    } else {
        [_seleM enumerateObjectsUsingBlock:^(LWDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if ([obj.goods_id isEqualToString:model.goods_id]) {
                obj.orderCount ++;
            }
        }];
    }
}

#pragma MARK - MD5加密
- (NSString *)hybnetworking_md5:(NSString *)string {
    if (string == nil || [string length] == 0) {
        return nil;
    }
    
    unsigned char digest[CC_MD5_DIGEST_LENGTH], i;
    CC_MD5([string UTF8String], (int)[string lengthOfBytesUsingEncoding:NSUTF8StringEncoding], digest);
    NSMutableString *ms = [NSMutableString string];
    
    for (i = 0; i < CC_MD5_DIGEST_LENGTH; i++) {
        [ms appendFormat:@"%02x", (int)(digest[i])];
    }
    
    return [ms copy];
}
//购物侧角标数量变化代理
- (void)changeBadge {
    
    __block NSInteger count = 0;
    
    //热卖商品
    [_hotGoodsList enumerateObjectsUsingBlock:^(LWGroupList * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        count += obj.orderCount;
    }];
    
    //最新上架
    [_newGoodsList enumerateObjectsUsingBlock:^(LWGroupList * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        count += obj.orderCount;
    }];
    [_highGoodsList enumerateObjectsUsingBlock:^(LWGroupList * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        count += obj.orderCount;
    }];
    
    //优质商品
    [_promotionList enumerateObjectsUsingBlock:^(LWGroupList * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        count += obj.orderCount;
    }];
    [[NSNotificationCenter defaultCenter] postNotificationName:LWShopCarBadgeChangeNotification object:@(count)];
    
    
}
- (void)viewDidLoad {
    
    [super viewDidLoad];
    _seleM = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    
    //设置导航栏颜色
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithHex:0x9ACD32];
    
    //导航栏中间的按钮
    chooseCityBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200, 44)];
    
    [chooseCityBtn setTitle:@"临网商城" forState:UIControlStateNormal];
    
    [chooseCityBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    self.navigationItem.titleView = chooseCityBtn;
    
    [self setupUI];
    
    [self loadNetworking];
    
    [self loadData];
    
    
    //注册cell
    [self.tableView registerClass:[LWTopCell class] forCellReuseIdentifier:topcell];
    [self.tableView registerClass:[LWMidCell class] forCellReuseIdentifier:hotcell];
    [self.tableView registerClass:[LWNewestCell class] forCellReuseIdentifier:newCell];
    [self.tableView registerClass:[LWPromotionCell class] forCellReuseIdentifier:promotionCell];
    [self.tableView registerClass:[LWhigtGoodsCell class] forCellReuseIdentifier:highCell];
    
    //接收购物车发送的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(coordinateValue:) name:LWShopCarNotification object:nil];
}

#pragma mark - LWShopCarNotification
- (void)coordinateValue:(NSNotification *)noti {
    __block NSInteger count = 0;
    [_seleM enumerateObjectsUsingBlock:^(LWDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        count += obj.orderCount;
    }];
    self.tabBarController.viewControllers[2].tabBarItem.badgeValue = @(count).description;
    NSMutableArray<LWDetailModel *> *_deleArr = noti.userInfo[@"DeleteArr"];
    __block BOOL indicate = NO;
    [_deleArr enumerateObjectsUsingBlock:^(LWDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        for (LWGroupList *group in _hotGoodsList) {
            if ([group.goods_id isEqualToString:obj.goods_id]) {
                indicate = YES;
                group.orderCount = 0;
                break;
            }
        }
        for (LWGroupList *group in _newGoodsList) {
            if ([group.goods_id isEqualToString:obj.goods_id]) {
                indicate = YES;
                group.orderCount = 0;
                break;
            }
            if (indicate) {
                break;
            }
        }
        for (LWGroupList *group in _promotionList) {
            if ([group.goods_id isEqualToString:obj.goods_id]) {
                indicate = YES;
                group.orderCount = 0;
                break;
            }
            if (indicate) {
                break;
            }
        }
        for (LWGroupList *group in _highGoodsList) {
            if ([group.goods_id isEqualToString:obj.goods_id]) {
                group.orderCount = 0;
                break;
            }
            if (indicate) {
                break;
            }
        }
        indicate = NO;
    }];
    [self changeTableViewOrderCount];
    [self.tableView reloadData];
}
//修改tableView显示的购物数量
- (void)changeTableViewOrderCount {
    __block BOOL indicate = NO;
    [_seleM enumerateObjectsUsingBlock:^(LWDetailModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        for (LWGroupList *group in _hotGoodsList) {
            if ([group.goods_id isEqualToString:obj.goods_id]) {
                indicate = YES;
                group.orderCount = obj.orderCount;
                break;
            }
        }
        for (LWGroupList *group in _newGoodsList) {
            if ([group.goods_id isEqualToString:obj.goods_id]) {
                indicate = YES;
                group.orderCount = obj.orderCount;
                break;
            }
            if (indicate) {
                break;
            }
        }
        for (LWGroupList *group in _promotionList) {
            if ([group.goods_id isEqualToString:obj.goods_id]) {
                indicate = YES;
                group.orderCount = obj.orderCount;
                break;
            }
            if (indicate) {
                break;
            }
        }
        for (LWGroupList *group in _highGoodsList) {
            if ([group.goods_id isEqualToString:obj.goods_id]) {
                group.orderCount = obj.orderCount;
                break;
            }
            if (indicate) {
                break;
            }
        }
        indicate = NO;
    }];
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma MARK - UI
- (void)setupUI
{
    UIView *topV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 140)];
    
    //  self.tableView.tableHeaderView = topV;
    ((UITableView *)self.view).tableHeaderView = topV;
    
    // 网络加载 --- 创建不带标题的图片轮播器
    SDCycleScrollView *cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0,self.view.bounds.size.width ,150) delegate:self placeholderImage:[UIImage imageNamed:@""]];
    
    self.sdcycleScrollerView = cycleScrollView;
    
    cycleScrollView.delegate = self;
    
    cycleScrollView.pageControlStyle = SDCycleScrollViewPageContolAlimentRight;
    
    cycleScrollView.currentPageDotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
    
    [topV addSubview:cycleScrollView];
    
    cycleScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    cycleScrollView.autoScrollTimeInterval = 3.0;
    
    //搜索
    UIBarButtonItem *searchBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_search"] style:UIBarButtonItemStylePlain target:self action:@selector(jumpToSearch)];
    
    self.navigationItem.rightBarButtonItem = searchBtn;
    
    //扫一扫
    UIBarButtonItem *sweepBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"icon_black_scancode"] style:UIBarButtonItemStylePlain target:self action:@selector(jumpToSweep)];
    
    self.navigationItem.leftBarButtonItem = sweepBtn;
    
}

#pragma mark - 首页数据网络请求
- (void)loadNetworking {
    
    __weak __typeof(self)weakSelf = self;
    
    //轮播
    [HYBNetworking getWithUrl:@"index.php?m=Api&c=Index&a=homePage" refreshCache:YES success:^(id response) {
        
        LWMainModel *mainModel = [LWMainModel yy_modelWithJSON:response];
        
        if (mainModel.status == 1) {
            
            LWHomeModel *homeModel = [LWHomeModel yy_modelWithJSON:mainModel.result];
            
            NSMutableArray *pictures = [NSMutableArray array];
            for (LWHomePictureModel *pictureModel in [NSArray yy_modelArrayWithClass:[LWHomePictureModel class] json:homeModel.ad]) {
                
                [pictures addObject:pictureModel.ad_code];
                
            }
            
            weakSelf.sdcycleScrollerView.imageURLStringsGroup = pictures;
            
        }
        
        
    } fail:^(NSError *error) {
        
        NSLog(@"%@",error);
        
    }];
    
}

#pragma mark - SDCycleScrollViewDelegate

- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"---点击了第%ld张图片", (long)index);
    
}

#pragma mark - 搜索页面
-(void)jumpToSearch
{
    NSLog(@"点击了搜索按钮");
    
    
}
- (void)jumpToSweep
{
    NSLog(@"点击了扫一扫按钮");
    LWRichScanVc *vc = [LWRichScanVc new];
    
    [self.navigationController pushViewController:vc animated:YES];
   
    
}
#pragma mark - 数据层
- (void)loadData
{
    NSURL *URL = [NSURL URLWithString:@"http://shop.sdlinwang.com/index.php?m=Api&c=Index&a=homePage"];
    
    [[[NSURLSession sharedSession]dataTaskWithURL:URL completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
        if (error == nil && data != nil) {
            
            //主要
            NSDictionary *dictArr = [NSJSONSerialization JSONObjectWithData:data options:0 error:NULL];
            
            
            //热卖食品
            NSArray *array = dictArr[@"result"][@"hot_goods"];
            NSMutableArray *tmpM = [NSMutableArray arrayWithCapacity:array.count];
            
            [array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                LWGroupList * list = [LWGroupList groupWithDict:obj];
                
                
                [tmpM addObject:list];
                
            }];
            
            _hotGoodsList = tmpM.copy;
            
            
            // 最新食品
            NSArray *newarray = dictArr[@"result"][@"zuixin_goods"];
            
            NSMutableArray *newtepM = [NSMutableArray arrayWithCapacity:newarray.count];
            
            [newarray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                LWGroupList * list = [LWGroupList groupWithDict:obj];
                
                [newtepM addObject:list];
                
            }];
            
            _newGoodsList = newtepM.copy;
            
            
            //促销商品
            NSArray *promotionArray = dictArr[@"result"][@"promotion_goods"];
            
            NSMutableArray *promotionTepM = [NSMutableArray arrayWithCapacity:promotionArray.count];
            
            [promotionArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                LWGroupList *list = [LWGroupList groupWithDict:obj];
                
                [promotionTepM addObject:list];
                
            }];
            
            _promotionList = promotionTepM.copy;
            
            //优质商品   high_quality_goods
            NSArray *highArray = dictArr[@"result"][@"high_quality_goods"];
            
            NSMutableArray *highTepM = [NSMutableArray arrayWithCapacity:highArray.count];
            
            [highArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                LWGroupList *list = [LWGroupList groupWithDict:obj];
                
                [highTepM addObject:list];
                
            }];
            
            _highGoodsList = highTepM.copy;
            
            
            
            
            
            
            
            
            // 刷新列表 : (主线程)
            [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                [self.tableView reloadData];
            }];
            
            
        }else {
            
            NSLog(@"%@",error);
            
            
        }
        
    }]resume];
    
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (section == 0) {
        return 1;
        
    }else if (section == 1)
    {
        return _hotGoodsList.count;
    }else if (section == 2)
    {
        return _newGoodsList.count;
    }else if(section == 3)
    {
        return _promotionList.count;
    }else
    {
        return _highGoodsList.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (indexPath.section == 0) {
        LWTopCell *cell = [tableView dequeueReusableCellWithIdentifier:@"topcell" forIndexPath:indexPath];
        
        return cell;
    }else if (indexPath.section == 1)
    {
        LWMidCell *cell = [tableView dequeueReusableCellWithIdentifier:@"hotcell" forIndexPath:indexPath];
        
        cell.model = _hotGoodsList[indexPath.row];
        
        
        //设置代理
        ((LWMidCell *)cell).delegate = self;
        
        return cell;
        
    }else if (indexPath.section == 2)
    {
        LWNewestCell *cell = [tableView dequeueReusableCellWithIdentifier:@"newCell" forIndexPath:indexPath];
        
        cell.model = _newGoodsList[indexPath.row];
        
        //设置代理
        ((LWNewestCell *)cell).delegate = self;
        
        return cell;
        
    }else if(indexPath.section == 3)
    {
        LWPromotionCell *cell = [tableView dequeueReusableCellWithIdentifier:promotionCell forIndexPath:indexPath];
        
        cell.model = _promotionList[indexPath.row];
        
        //设置代理
        ((LWPromotionCell *)cell).delegate = self;
        
        return cell;
        
    }else
    {
        
        LWhigtGoodsCell *cell = [tableView dequeueReusableCellWithIdentifier:highCell forIndexPath:indexPath];
        
        cell.model = _highGoodsList[indexPath.row];
        
        //设置代理
        ((LWhigtGoodsCell *)cell).delegate = self;
        
        return cell;
    }
    
}
//代理方法
- (void)higtGoodsCell:(LWhigtGoodsCell *)higtGoodsCell didFinishIncreaseFood:(LWGroupList *)food andStartPoint:(CGPoint)starPoint
{
    //添加红点图片
    UIImageView *dotImage = [[UIImageView alloc] init];
    
    [dotImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://shop.sdlinwang.com/%@",food.original_img]]];
    dotImage.size = CGSizeMake(80, 80);
    
    dotImage.center = starPoint;
    
    UIWindow *keyW = [UIApplication sharedApplication].keyWindow;
    
    [keyW addSubview:dotImage];
    
    //开启动画
    CAKeyframeAnimation *anmin = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    
    //通过KVC绑定imageView
    [anmin setValue:dotImage forKey:@"imgV"];
    
    //设置路径
    UIBezierPath *Path = [UIBezierPath bezierPath];
    
    [Path moveToPoint:starPoint];
    
    //结束点
    CGPoint endP  = CGPointMake(270, keyW.bounds.size.height - 55);
    
    //控制点
    CGPoint controlP = CGPointMake(starPoint.x - 130,starPoint.y - 120);
    
    //添加曲线
    [Path addQuadCurveToPoint:endP controlPoint:controlP];
    
    anmin.path= Path.CGPath;
    
    //防止返回去
    anmin.removedOnCompletion = NO;
    anmin.fillMode = kCAFillModeForwards;
    
    anmin.duration = 1;
    
    //设置动画代理
    anmin.delegate = self;
    
    
    //添加路径
    [dotImage.layer addAnimation:anmin forKey:nil];

    
}
- (void)promotionCell:(LWPromotionCell *)promotionCell didFinishIncreaseFood:(LWGroupList *)food andStartPoint:(CGPoint)starPoint
{
    //添加红点图片
    UIImageView *dotImage = [[UIImageView alloc] init];
    
    [dotImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://shop.sdlinwang.com/%@",food.original_img]]];
    dotImage.size = CGSizeMake(80, 80);
    
    dotImage.center = starPoint;
    
    UIWindow *keyW = [UIApplication sharedApplication].keyWindow;
    
    [keyW addSubview:dotImage];
    
    //开启动画
    CAKeyframeAnimation *anmin = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    
    //通过KVC绑定imageView
    [anmin setValue:dotImage forKey:@"imgV"];
    
    //设置路径
    UIBezierPath *Path = [UIBezierPath bezierPath];
    
    [Path moveToPoint:starPoint];
    
    //结束点
    CGPoint endP  = CGPointMake(270, keyW.bounds.size.height - 55);
    
    //控制点
    CGPoint controlP = CGPointMake(starPoint.x - 130,starPoint.y - 120);
    
    //添加曲线
    [Path addQuadCurveToPoint:endP controlPoint:controlP];
    
    anmin.path= Path.CGPath;
    
    //防止返回去
    anmin.removedOnCompletion = NO;
    anmin.fillMode = kCAFillModeForwards;
    
    anmin.duration = 1;
    
    //设置动画代理
    anmin.delegate = self;
    
    
    //添加路径
    [dotImage.layer addAnimation:anmin forKey:nil];
    
}
- (void)newCell:(LWNewestCell *)newCell didFinishIncreaseFood:(LWGroupList *)food andStartPoint:(CGPoint)starPoint
{
    //添加红点图片
    UIImageView *dotImage = [[UIImageView alloc] init];
    
    [dotImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://shop.sdlinwang.com/%@",food.original_img]]];
    dotImage.size = CGSizeMake(80, 80);
    
    dotImage.center = starPoint;
    
    UIWindow *keyW = [UIApplication sharedApplication].keyWindow;
    
    [keyW addSubview:dotImage];
    
    //开启动画
    CAKeyframeAnimation *anmin = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    
    //通过KVC绑定imageView
    [anmin setValue:dotImage forKey:@"imgV"];
    
    //设置路径
    UIBezierPath *Path = [UIBezierPath bezierPath];
    
    [Path moveToPoint:starPoint];
    
    //结束点
    CGPoint endP  = CGPointMake(270, keyW.bounds.size.height - 55);
    
    //控制点
    CGPoint controlP = CGPointMake(starPoint.x - 130,starPoint.y - 120);
    
    //添加曲线
    [Path addQuadCurveToPoint:endP controlPoint:controlP];
    
    anmin.path= Path.CGPath;
    
    //防止返回去
    anmin.removedOnCompletion = NO;
    anmin.fillMode = kCAFillModeForwards;
    
    anmin.duration = 1;
    
    //设置动画代理
    anmin.delegate = self;
    
    
    //添加路径
    [dotImage.layer addAnimation:anmin forKey:nil];
    
}
- (void)midCell:(LWMidCell *)midCell didFinishIncreaseFood:(LWGroupList *)food andStartPoint:(CGPoint)starPoint
{
    NSLog(@"%@ 增加菜品  动画",food);
    //添加红点图片
    UIImageView *dotImage = [[UIImageView alloc] init];
    
    [dotImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://shop.sdlinwang.com/%@",food.original_img]]];
    
    //切合圆角
    dotImage.layer.cornerRadius = 5;
    
    
    dotImage.size = CGSizeMake(80, 80);
    
    
    dotImage.center = starPoint;
    
    UIWindow *keyW = [UIApplication sharedApplication].keyWindow;
    
    [keyW addSubview:dotImage];
    
    
    
    //开启动画
    CAKeyframeAnimation *anmin = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    
    //通过KVC绑定imageView
    [anmin setValue:dotImage forKey:@"imgV"];
    
    //设置路径
    UIBezierPath *Path = [UIBezierPath bezierPath];
    
    [Path moveToPoint:starPoint];
    
    //结束点
    CGPoint endP  = CGPointMake(270, keyW.bounds.size.height - 55);
    
    //控制点
    CGPoint controlP = CGPointMake(starPoint.x - 130,starPoint.y - 120);
    
    //添加曲线
    [Path addQuadCurveToPoint:endP controlPoint:controlP];
    
    anmin.path= Path.CGPath;
    
    //防止返回去
    anmin.removedOnCompletion = NO;
    anmin.fillMode = kCAFillModeForwards;
    
    anmin.duration = 1;
    
    //设置动画代理
    anmin.delegate = self;
    
    
    //添加路径
    [dotImage.layer addAnimation:anmin forKey:nil];
    
    
}
//动画的代理方法
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    //移除
    UIImageView *imgV = [anim valueForKey:@"imgV"];
    
    //移除诶你动画
    [imgV.layer removeAllAnimations];
    
    [imgV removeFromSuperview];
    
}
- (void)midCell:(LWMidCell *)midCell didFinishDecreaseFood:(LWGroupList *)food
{
    NSLog(@"%@ 减少菜品  不要动画", food);
}
#pragma  MARK - TAB代理方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"点击了第%ld行",(long)indexPath.row);
    LWDetailsViewController *detailVc = [LWDetailsViewController new];
    
    if (indexPath.section == 1) {
        
        
        NSLog(@"点击了第%ld组，第%ld行",(long)indexPath.section, (long)indexPath.row);
        
        //        LWDetailsViewController *detailVc = [LWDetailsViewController new];
        
        LWDetailModel *model = _hotGoodsList[indexPath.row];
        
        
        detailVc.itemId = model.goods_id;
        
        
        
    }else if (indexPath.section == 2)
    {
        NSLog(@"点击了第%ld组，第%ld行",(long)indexPath.section, (long)indexPath.row);
        
        //        LWDetailsViewController *detailVc = [LWDetailsViewController new];
        
        LWDetailModel *model = _newGoodsList[indexPath.row];
        
        
        detailVc.itemId = model.goods_id;
        
        
        
        
    }else if (indexPath.section == 3)
    {
        NSLog(@"点击了第%ld组，第%ld行",(long)indexPath.section, (long)indexPath.row);
        
        LWDetailModel *model = _promotionList[indexPath.row];
        
        
        detailVc.itemId = model.goods_id;
        
        
        
    }else
    {
        NSLog(@"点击了第%ld组，第%ld行",(long)indexPath.section, (long)indexPath.row);
        
        
        LWDetailModel *model = _highGoodsList[indexPath.row];
        
        detailVc.itemId = model.goods_id;
    }
    
    
    
    //push 之前隐藏底部tabbar
    detailVc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detailVc animated:YES];
    
    //push 之后显示tabbar
    
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        
        return @"热卖商品";
        
    }else if (section == 2)
    {
        return @"最新上架";
        
    }else if(section == 3)
        
    {
        return @"促销商品";
        
    }else if(section == 4)
        
    {
        return @"优质商品";
    }else
    {
        return nil;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat rowHieght;
    if (indexPath.section == 0) {
        rowHieght = 320;
    }
    else
    {
        rowHieght = 120;
    }
    return  rowHieght;
}
@end
